package com.lcf.user.controller;

import com.lcf.user.dto.ClusterStorageDto;
import com.lcf.user.entity.PortraitLib;
import com.lcf.user.mapper.PortraitLibMapper;
import com.lcf.user.vo.ClusterStorageVo;
import com.lcf.user.vo.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sensetime/cluster")
@Api(value = "人像库服务", tags = {"PortraitLib Service"})
public class PortraitLibController {
    @Autowired
    private PortraitLibMapper portraitLibMapper;

    @PostMapping("/create")
    @ApiOperation(value = "创建人像库", notes = "创建人像库aa", response = Integer.class)
    public int create(@RequestBody PortraitLib portraitLib) {
        int insert = portraitLibMapper.insert(portraitLib);
        return insert;
    }

    @GetMapping("/delete/{storageId}")
    @ApiOperation(value = "删除人像库", notes = "删除人像库aa", response = Integer.class)
    public int deleteByPrimaryKey(@PathVariable Long storageId) {
        int i = portraitLibMapper.deleteByPrimaryKey(storageId);
        return i;
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新人像库", notes = "更新人像库aa", response = Integer.class)
    public int update(@RequestBody PortraitLib portraitLib) {
        int i = portraitLibMapper.updateByPrimaryKey(portraitLib);
        return i;
    }

    @GetMapping("/findPortraitLibByName/{storageName}")
    @ApiOperation(value = "搜索人像库", notes = "搜索人像库aa", response = PortraitLib.class)
    public PortraitLib findPortraitLibByName(@PathVariable String storageName) {
        PortraitLib portraitLib = portraitLibMapper.findPortraitLibByName(storageName);
        return portraitLib;
    }


    @GetMapping("/setting/info")
    @ApiOperation(value = "查询当前的聚类任务信息", notes = "返回启用和禁用状态的库的集合", response = ResultVo.class)
    public ResultVo settinginfo() {
        Map<String, List<PortraitLib>> resultMap = new HashMap<>();
        List<PortraitLib> portraitLibList = portraitLibMapper.findAllPortraitLib();

        List<PortraitLib> openList = new ArrayList<>();
        List<PortraitLib> shutList = new ArrayList<>();
        portraitLibList.forEach(portraitLib -> {
            //启用
            if (portraitLib.getStatus() == 1) {
                openList.add(portraitLib);
            } else {
                shutList.add(portraitLib);
            }
        });
        resultMap.put("openClusterStorageIds", openList);
        resultMap.put("shutClusterStorageIds", shutList);
        ResultVo<Object> objectResultVo = new ResultVo<>();
        objectResultVo.setCode(200);
        objectResultVo.setMsg("success");
        objectResultVo.setData(resultMap);
        return objectResultVo;
    }

    @PostMapping("/setting")
    @ApiOperation(value = "配置需要聚类的库", notes = "更新库是否启用", response = ResultVo.class)
    public ResultVo setting(@RequestBody ClusterStorageVo clusterStorageVo) {
        List<String> openClusterStorageIds = clusterStorageVo.getOpenClusterStorageIds();

        List<String> shutClusterStorageIds = clusterStorageVo.getShutClusterStorageIds();
        ClusterStorageDto clusterStorageDto = new ClusterStorageDto();
        for (String openClusterStorageId : openClusterStorageIds) {
            PortraitLib portraitDto = new PortraitLib();
            portraitDto.setStatus(1);
            portraitDto.setStorageId(Integer.valueOf(openClusterStorageId));
            clusterStorageDto.getPortraitDtoList().add(portraitDto);
        }

        for (String shutClusterStorageId : shutClusterStorageIds) {
            PortraitLib portraitDto = new PortraitLib();
            portraitDto.setStatus(0);
            portraitDto.setStorageId(Integer.valueOf(shutClusterStorageId));
            clusterStorageDto.getPortraitDtoList().add(portraitDto);
        }
        int i = portraitLibMapper.batchUpdate(clusterStorageDto.getPortraitDtoList());

        ResultVo<Object> objectResultVo = new ResultVo<>();
        objectResultVo.setCode(200);
        objectResultVo.setMsg("success");
        objectResultVo.setData("i= " + i);
        return objectResultVo;
    }

}
